package com.stu.utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class JDBCUtil {
	private static String dbDriver;
	private static String dbURL;
	private static String dbName;
	private static String dbPassword;
	static{
		
		try {
			InputStream in=JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
			Properties p=new Properties();
			p.load(in);
			dbDriver=p.getProperty("driverclass");
			dbURL=p.getProperty("url");
			dbName=p.getProperty("user");
			dbPassword=p.getProperty("password");
			Class.forName(dbDriver);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Connection getConnection() throws SQLException{
		
		return DriverManager.getConnection(dbURL, dbName, dbPassword);
	}
	public static void release(ResultSet rs,Statement st,Connection conn){
		if (rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			rs=null;
		}
		if (st!=null) {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			st=null;
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			conn=null;
		}
	}
}
